/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable @typescript-eslint/naming-convention */
import { IconArrowLeftFromArc, IconTimeline } from "@tabler/icons-react"
import React from "react"

export enum customNodeType {
	// 开始
	Start = "1",
	// 大模型
	LLM = "2",
	// 消息回复
	ReplyMessage = "3",
	// 选择器
	If = "4",
	// 代码
	Code = "5",
	// 向量
	Vector = "6",
	// 神奇表格
	Table = "7",
	// 加载器
	Loader = "8",
	// 变量
	Variable = "9",
	// HTTP请求
	HTTP = "10",
	// 子流程
	Sub = "11",
	// 结束
	End = "12",
	// 知识检索
	Knowledge = "13",
	// 意图识别
	IntentionRecognition = "14",
	// 工具
	Tools = "15",
	// 变量保存
	VariableSave = "21",
	// 循环
	Loop = "30",
	// 循环体
	LoopBody = "31",
	// 循环终止
	LoopEnd = "32",
	// 人员检索
	SearchUsers = "28",
	// 委托节点
	Agent = "29",
}

export const templateMap = {
	[customNodeType.Start]: {
		v0: {
			node_id: "MAGIC-FLOW-NODE-663c385e5a83f4-29733783",
			name: "开始",
			description: "",
			node_type: 1,
			meta: [],
			params: {
				branches: [
					{
						trigger_type: 1,
						next_nodes: [],
						branch_id: "1",
						config: null,
						input: {
							widget: null,
							form: {
								id: "component-663c385e5b541",
								version: "1",
								type: "form",
								structure: {
									type: "object",
									key: "root",
									sort: 0,
									title: "root节点",
									description: "",
									required: ["nickname", "chat_time", "message_type", "content"],
									value: null,
									items: null,
									properties: {
										nickname: {
											type: "string",
											key: "nickname",
											sort: 0,
											title: " 用户昵称",
											description: "",
											required: null,
											value: null,
											items: null,
											properties: null,
										},
										chat_time: {
											type: "string",
											key: "chat_time",
											sort: 1,
											title: "发送时间",
											description: "",
											required: null,
											value: null,
											items: null,
											properties: null,
										},
										message_type: {
											type: "string",
											key: "message_type",
											sort: 2,
											title: "消息类型",
											description: "",
											required: null,
											value: null,
											items: null,
											properties: null,
										},
										content: {
											type: "string",
											key: "content",
											sort: 3,
											title: "消息类容",
											description: "",
											required: null,
											value: null,
											items: null,
											properties: null,
										},
									},
								},
							},
						},
						output: {
							widget: null,
							form: {
								id: "component-663c385e5b541",
								version: "1",
								type: "form",
								structure: {
									type: "object",
									key: "root",
									sort: 0,
									title: "root节点",
									description: "",
									required: ["nickname", "chat_time", "message_type", "content"],
									value: null,
									items: null,
									properties: {
										nickname: {
											type: "string",
											key: "nickname",
											sort: 0,
											title: " 用户昵称",
											description: "",
											required: null,
											value: null,
											items: null,
											properties: null,
										},
										chat_time: {
											type: "string",
											key: "chat_time",
											sort: 1,
											title: "发送时间",
											description: "",
											required: null,
											value: null,
											items: null,
											properties: null,
										},
										message_type: {
											type: "string",
											key: "message_type",
											sort: 2,
											title: "消息类型",
											description: "",
											required: null,
											value: null,
											items: null,
											properties: null,
										},
										content: {
											type: "string",
											key: "content",
											sort: 3,
											title: "消息类容",
											description: "",
											required: null,
											value: null,
											items: null,
											properties: null,
										},
									},
								},
							},
						},
					},
					{
						trigger_type: 2,
						next_nodes: [],
						branch_id: "2",
						config: null,
						input: {
							widget: null,
							form: {
								id: "component-663c385e5b7f1",
								version: "1",
								type: "form",
								structure: {
									type: "object",
									key: "root",
									sort: 0,
									title: "root节点",
									description: "",
									required: ["nickname", "open_time"],
									value: null,
									items: null,
									properties: {
										nickname: {
											type: "string",
											key: "nickname",
											sort: 0,
											title: " 用户昵称",
											description: "",
											required: null,
											value: null,
											items: null,
											properties: null,
										},
										open_time: {
											type: "string",
											key: "open_time",
											sort: 1,
											title: "打开时间",
											description: "",
											required: null,
											value: null,
											items: null,
											properties: null,
										},
									},
								},
							},
						},
						output: {
							widget: null,
							form: {
								id: "component-663c385e5b7f1",
								version: "1",
								type: "form",
								structure: {
									type: "object",
									key: "root",
									sort: 0,
									title: "root节点",
									description: "",
									required: ["nickname", "open_time"],
									value: null,
									items: null,
									properties: {
										nickname: {
											type: "string",
											key: "nickname",
											sort: 0,
											title: " 用户昵称",
											description: "",
											required: null,
											value: null,
											items: null,
											properties: null,
										},
										open_time: {
											type: "string",
											key: "open_time",
											sort: 1,
											title: "打开时间",
											description: "",
											required: null,
											value: null,
											items: null,
											properties: null,
										},
									},
								},
							},
						},
					},
				],
			},
			next_nodes: [],
			input: null,
			output: null,
		},
	},
	[customNodeType.If]: {
		v0: {
			node_id: "MAGIC-FLOW-NODE-66c444c8ca1934-29144044",
			name: "条件判断",
			description: "",
			node_type: 4,
			meta: [],
			params: {
				max_execute_num: 1,
				branches: [
					{
						branch_id: "branch_66c444c8ca355",
						branch_type: "if",
						next_nodes: [],
						parameters: {
							id: "component-66c444c8ca73e",
							version: "1",
							type: "condition",
							structure: null,
						},
					},
					{
						branch_id: "branch_66c444c8ca7d5",
						branch_type: "else",
						next_nodes: [],
						parameters: null,
					},
				],
			},
			next_nodes: [],
			input: null,
			output: null,
		},
	},
	[customNodeType.Sub]: {
		v0: {
			node_id: "MAGIC-FLOW-NODE-6649da5637d546-69160385",
			name: "子流程",
			description: "",
			node_type: 11,
			meta: [],
			params: {
				sub_flow_id: "MAGIC-FLOW-6629fe3e606e03-15176825",
			},
			next_nodes: [],
			input: {
				widget: null,
				form: {
					id: "component-662617c1a0884",
					version: "1",
					type: "form",
					structure: {
						type: "object",
						key: "root",
						sort: 0,
						title: null,
						description: null,
						required: ["input"],
						value: null,
						items: null,
						properties: {
							input: {
								type: "string",
								key: "input",
								sort: 0,
								title: "输入",
								description: "",
								required: null,
								value: null,
								items: null,
								properties: null,
							},
						},
					},
				},
			},
			output: {
				widget: null,
				form: {
					id: "component-662617a69868d",
					version: "1",
					type: "form",
					structure: {
						type: "object",
						key: "root",
						sort: 0,
						title: null,
						description: null,
						required: [],
						value: null,
						items: null,
						properties: {
							output: {
								type: "string",
								key: "output",
								sort: 0,
								title: "输出",
								description: "",
								required: null,
								value: {
									type: "expression",
									const_value: null,
									expression_value: [
										{
											type: "fields",
											value: "MAGIC-FLOW-NODE-6629f9ee4d6248-06936049.output",
											name: "output",
											args: null,
										},
									],
								},
								items: null,
								properties: null,
							},
						},
					},
				},
			},
		},

		v1: {
			node_id: "MAGIC-FLOW-NODE-6649da5637d546-69160385",
			name: "子流程版本2",
			description: "",
			node_type: 11,
			meta: [],
			params: {
				sub_flow_id: "MAGIC-FLOW-6629fe3e606e03-15176825",
			},
			next_nodes: [],
			input: {
				widget: null,
				form: {
					id: "component-662617c1a0884",
					version: "1",
					type: "form",
					structure: {
						type: "object",
						key: "root",
						sort: 0,
						title: null,
						description: null,
						required: ["input"],
						value: null,
						items: null,
						properties: {
							input: {
								type: "string",
								key: "input",
								sort: 0,
								title: "输入",
								description: "",
								required: null,
								value: null,
								items: null,
								properties: null,
							},
						},
					},
				},
			},
			output: {
				widget: null,
				form: {
					id: "component-662617a69868d",
					version: "1",
					type: "form",
					structure: {
						type: "object",
						key: "root",
						sort: 0,
						title: null,
						description: null,
						required: [],
						value: null,
						items: null,
						properties: {
							output_v2: {
								type: "number",
								key: "output",
								sort: 0,
								title: "输出",
								description: "",
								required: null,
								value: {
									type: "expression",
									const_value: null,
									expression_value: [
										{
											type: "fields",
											value: "MAGIC-FLOW-NODE-6629f9ee4d6248-06936049.output",
											name: "output",
											args: null,
										},
									],
								},
								items: null,
								properties: null,
							},
						},
					},
				},
			},
		},
	},

	[customNodeType.Loop]: {
		v0: {
			node_id: "MAGIC-FLOW-NODE-66a70a8da8ed48-26322039",
			name: "循环",
			description: "",
			node_type: 22,
			meta: [],
			params: {
				type: "array", // 计数类型：count 循环数组：array 条件循环：condition,
				condition: {
					// 条件循环时启用, 用于给定一个终止循环条件
					id: "component-66da72180d2e1",
					version: "1",
					type: "condition",
					structure: undefined,
				},
				count: {
					// 计数循环时启用
					id: "component-66da73d27fea0",
					version: "1",
					type: "value",
					structure: null,
				},
				array: {
					// 遍历循环数组时启用
					id: "component-66da73d27fea0",
					version: "1",
					type: "value",
					structure: null,
				},
				max_loop_count: {
					// 最大遍历次数限制，条件循环时启用，可以允许为空
					id: "component-66da73d27fea0",
					version: "1",
					type: "value",
					structure: null,
				},
			},
			next_nodes: [],
			input: null,
			output: null,
		},
	},
	[customNodeType.LoopEnd]: {
		v0: {
			node_id: "MAGIC-FLOW-NODE-66a70a8da8ed48-2632203d",
			name: "循环终止",
			description: "",
			node_type: 24,
			meta: [],
			params: {},
			next_nodes: [],
			input: null,
			output: null,
		},
	},
	[customNodeType.LoopBody]: {
		v0: {
			node_id: "MAGIC-FLOW-NODE-66a70a8da8ed48-2632203f",
			name: "循环体",
			description: "",
			node_type: 23,
			meta: [],
			params: {},
			next_nodes: [],
			input: null,
			output: null,
		},
	},
	[customNodeType.VariableSave]: {
		v0: {
			node_id: "MAGIC-FLOW-NODE-66a2a8453cfd12-52796719",
			name: "变量设置",
			description: "",
			node_type: 21,
			meta: [],
			params: {
				variables: {
					form: {
						id: "component-66a2a8453cfe4",
						version: "1",
						type: "form",
						structure: {
							type: "object",
							key: "root",
							sort: 0,
							title: "root节点",
							description: null,
							required: ["variable_name", "variable_type"],
							value: null,
							items: null,
							properties: {
								variable_name: {
									type: "string",
									key: "variable_name",
									sort: 0,
									title: "变量名",
									description: "",
									required: null,
									value: null,
									items: null,
									properties: null,
								},
								variable_type: {
									type: "string",
									key: "variable_type",
									sort: 1,
									title: "变量类型",
									description: "",
									required: null,
									value: null,
									items: null,
									properties: null,
								},
							},
						},
					},
					page: null,
				},
			},
			next_nodes: [],
			input: null,
			output: null,
		},
	},
	[customNodeType.LLM]: {
		v0: {
			node_id: "MAGIC-FLOW-NODE-6753cb5235e676-30935789",
			debug: false,
			name: "LLM Chat",
			description: "",
			node_type: 2,
			meta: [],
			params: {
				model: "gpt-4o-global",
				system_prompt: {
					id: "component-6753cb5235e78",
					version: "1",
					type: "value",
					structure: {
						type: "expression",
						const_value: null,
						expression_value: null,
					},
				},
				user_prompt: {
					id: "component-6753cb5235ea2",
					version: "1",
					type: "value",
					structure: {
						type: "expression",
						const_value: null,
						expression_value: null,
					},
				},
				model_config: {
					auto_memory: true,
					max_record: 50,
					temperature: 0.5,
				},
				tools: [],
				option_tools: [],
				knowledge_config: {
					operator: "developer",
					knowledge_list: [
						{
							knowledge_code: "",
							knowledge_type: 2,
							business_id: "",
							name: "",
							description: "",
						},
					],
					limit: 5,
					score: 0.4,
				},
				messages: {
					id: "component-6753cb5235ec6",
					version: "1",
					type: "form",
					structure: {
						type: "array",
						key: "root",
						sort: 0,
						title: "历史消息",
						description: "",
						required: null,
						value: null,
						encryption: false,
						encryption_value: null,
						items: {
							type: "object",
							key: "messages",
							sort: 0,
							title: "历史消息",
							description: "",
							required: ["role", "content"],
							value: null,
							encryption: false,
							encryption_value: null,
							items: null,
							properties: {
								role: {
									type: "string",
									key: "role",
									sort: 0,
									title: "角色",
									description: "",
									required: null,
									value: null,
									encryption: false,
									encryption_value: null,
									items: null,
									properties: null,
								},
								content: {
									type: "string",
									key: "content",
									sort: 1,
									title: "内容",
									description: "",
									required: null,
									value: null,
									encryption: false,
									encryption_value: null,
									items: null,
									properties: null,
								},
							},
						},
						properties: null,
					},
				},
			},
			next_nodes: [],
			input: null,
			output: {
				widget: null,
				form: {
					id: "component-6753cb5235f3e",
					version: "1",
					type: "form",
					structure: {
						type: "object",
						key: "root",
						sort: 0,
						title: "root节点",
						description: "",
						required: ["text", "use_tools"],
						value: null,
						encryption: false,
						encryption_value: null,
						items: null,
						properties: {
							use_tools: {
								type: "array",
								key: "use_tools",
								sort: 0,
								title: "使用到的工具",
								description: "",
								required: null,
								value: null,
								encryption: false,
								encryption_value: null,
								items: {
									type: "object",
									key: "",
									sort: 0,
									title: "工具详情",
									description: "",
									required: [],
									value: null,
									encryption: false,
									encryption_value: null,
									items: null,
									properties: {
										tool_name: {
											type: "string",
											key: "tool_name",
											sort: 0,
											title: "工具名称",
											description: "",
											required: null,
											value: null,
											encryption: false,
											encryption_value: null,
											items: null,
											properties: null,
										},
										success: {
											type: "boolean",
											key: "success",
											sort: 1,
											title: "是否成功",
											description: "",
											required: null,
											value: null,
											encryption: false,
											encryption_value: null,
											items: null,
											properties: null,
										},
										error_message: {
											type: "string",
											key: "error_message",
											sort: 2,
											title: "错误信息",
											description: "",
											required: null,
											value: null,
											encryption: false,
											encryption_value: null,
											items: null,
											properties: null,
										},
										arguments: {
											type: "object",
											key: "arguments",
											sort: 3,
											title: "工具参数",
											description: "",
											required: [],
											value: null,
											encryption: false,
											encryption_value: null,
											items: null,
											properties: null,
										},
										call_result: {
											type: "string",
											key: "call_result",
											sort: 4,
											title: "调用结果",
											description: "",
											required: null,
											value: null,
											encryption: false,
											encryption_value: null,
											items: null,
											properties: null,
										},
										elapsed_time: {
											type: "string",
											key: "elapsed_time",
											sort: 5,
											title: "耗时",
											description: "",
											required: null,
											value: null,
											encryption: false,
											encryption_value: null,
											items: null,
											properties: null,
										},
									},
								},
								properties: null,
							},
							text: {
								type: "string",
								key: "text",
								sort: 1,
								title: "LLM 响应文本",
								description: "",
								required: null,
								value: null,
								encryption: false,
								encryption_value: null,
								items: null,
								properties: null,
							},
						},
					},
				},
			},
			system_output: null,
		},
	},
	[customNodeType.Tools]: {
		v0: {
			node_id: "MAGIC-FLOW-NODE-674c4f70bc3966-92064256",
			debug: false,
			name: "工具",
			description: "",
			node_type: 26,
			meta: [],
			params: {
				tool_id: "",
				mode: "llm",
				custom_system_input: {
					widget: null,
					form: {
						id: "component-674c4f70bc3a8",
						version: "1",
						type: "form",
						structure: {
							type: "object",
							key: "root",
							sort: 0,
							title: null,
							description: null,
							required: [],
							value: null,
							encryption: false,
							encryption_value: null,
							items: null,
							properties: null,
						},
					},
				},
				async: false,
				model: "gpt-4o-global",
				model_config: {
					auto_memory: false,
					max_record: 50,
					temperature: 0.5,
				},
				user_prompt: {
					id: "component-674c4f70bc485",
					version: "1",
					type: "value",
					structure: {
						type: "expression",
						const_value: null,
						expression_value: null,
					},
				},
			},
			next_nodes: [],
			input: {
				widget: null,
				form: null,
			},
			output: null,
			system_output: null,
		},
	},
}

function addVersionKeyToNodes(nodes: Record<string, any>): Record<string, any> {
	return Object.fromEntries(Object.entries(nodes).map(([key, value]) => [key, { v0: value }]))
}

export const nodeSchemaMap = {
	[customNodeType.Start]: {
		schema: {
			label: "开始节点",
			icon: <IconArrowLeftFromArc color="#fff" stroke={2} size={18} />,
			color: "#315CEC",
			id: customNodeType.Start,
			desc: "当以下事件被触发时，流程将会从这个模块开始执行",
			handle: {
				withSourceHandle: false,
				withTargetHandle: false,
			},
			changeable: {
				nodeType: false,
			},
			style: {
				width: "480px",
			},
			// params: templateMap[customNodeType.Start].params,
			// output: templateMap[customNodeType.Start].output,
		},
		component: () => <div />,
	},

	[customNodeType.Sub]: {
		schema: {
			label: "子流程",
			icon: <IconTimeline color="#fff" stroke={1} size={18} />,
			color: "#9DC900",
			id: customNodeType.Sub,
			desc: "可以将部分功能模块分配给子流程来进行编排，从而避免主流程过于庞大",
			handle: {
				withSourceHandle: true,
				withTargetHandle: true,
			},
			style: {
				width: "800px",
			},
			// params: templateMap[customNodeType.Sub].params,
			// output: templateMap[customNodeType.Sub].output,
		},
		component: () => <div />,
	},
}

export default nodeSchemaMap
